<?php
/**
 * File renderer.
 *
 * @package ElggFile
 */

$full = elgg_extract('full_view', $vars, false);
$message = elgg_extract('entity', $vars, false);

if (!$message) {
    return true;
}

if ($message->toId == elgg_get_page_owner_guid()) {
    // received
    $user = get_entity($message->fromId);
    if ($user) {
        $icon = elgg_view_entity_icon($user, 'tiny');
        $user_link = elgg_view('output/url', array(
            'href' => "messages/compose?send_to=$user->guid",
            'text' => $user->name,
            'is_trusted' => true,
        ));
    } else {
        $icon = '';
        $user_link = elgg_echo('messages:deleted_sender');
    }

    if ($message->readYet) {
        $class = 'message read';
    } else {
        $class = 'message unread';
    }

} else {
    // sent
    $user = get_entity($message->toId);

    if ($user) {
        $icon = elgg_view_entity_icon($user, 'tiny');
        $user_link = elgg_view('output/url', array(
            'href' => "messages/compose?send_to=$user->guid",
            'text' => elgg_echo('messages:to_user', array($user->name)),
            'is_trusted' => true,
        ));
    } else {
        $icon = '';
        $user_link = elgg_echo('messages:deleted_sender');
    }

    $class = 'message read';
}

$timestamp = elgg_view_friendly_time($message->time_created);

$subject_info = '';
if (!$full) {
    $subject_info .= "<input type='checkbox' name=\"message_id[]\" value=\"{$message->guid}\" />";
}
$subject_info .= elgg_view('output/url', array(
    'href' => $message->getURL(),
    'text' => $message->title,
    'is_trusted' => true,
));

$delete_link = elgg_view("output/confirmlink", array(
    'href' => "action/messages/delete?guid=" . $message->getGUID(),
    'text' => "<span class=\"elgg-icon elgg-icon-delete float-alt\"></span>",
    'confirm' => elgg_echo('deleteconfirm'),
    'encode_text' => false,
));

$body = <<<HTML
<div class="messages-owner">$user_link</div>
<div class="messages-subject">$subject_info</div>
<div class="messages-timestamp">$timestamp</div>
<div class="messages-delete">$delete_link</div>
HTML;

if ($full) {
    echo elgg_view_image_block($icon, $body, array('class' => $class));

    if ($message->svn) {
        $group = get_entity($message->svn);
        // button to create repo and add user authentication (action)
        $url = 'action/groups/approvesvn?approved=1&group_guid=' . $group->getGUID() . '&message_guid=' . $message->getGUID();
        $url = elgg_add_action_tokens_to_url($url);

        $approve_link = elgg_view('output/url', array(
            'href' => $url,
            'class' => 'elgg-button elgg-button-action',
            'text' => 'Chấp nhận yêu cầu này',
        ));

        $url = elgg_get_site_url() . "action/groups/approvesvn?approved=none&group_guid=" . $group->getGUID() . "&message_guid={$message->getGUID()}";;
        $url = elgg_add_action_tokens_to_url($url);

        $disapprove_link = elgg_view('output/url', array(
            'href' => $url,
            'class' => 'elgg-button elgg-button-cancel',
            'text' => 'Từ chối yêu cầu này',
        ));;


        $group_link = elgg_view('output/url', array(
            'text' => $group->name,
            'href' => 'groups/profile/' . $message->svn
        ));

        $group_text = elgg_echo('groups:svn_request_message', array($group_link));

        echo $group_text;
        echo '<hr/>';
        if (!$message->is_svn_approved) {
            echo $approve_link;
            echo ' ';
            echo $disapprove_link;
        } else {
            if ($message->is_svn_approved == "approved") {
                echo 'Nhóm ' . $group_link . ' đã được cấp kho.';
            } else {
                echo 'Nhóm ' . $group_link . ' đã bị từ chối.';
            }
        }
    } else {
        echo elgg_view('output/longtext', array('value' => $message->description));
    }
} else {
    echo elgg_view_image_block($icon, $body, array('class' => $class));
}